//
//  MDDAddressViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/3/17.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "MDDAddressViewController.h"
#import "AddressListCell.h"
#import "MDDEditAddressViewController.h"
#import "DLBaiduMapViewController.h"


@interface MDDAddressViewController ()
@property (weak, nonatomic) IBOutlet UITableView *selfTableView;
@property (nonatomic,strong) NSMutableArray *listArr;

@end

@implementation MDDAddressViewController
-(void)rightAction:(UIButton *)sender
{
//    DLBaiduMapViewController *mapVC = [[DLBaiduMapViewController alloc]init];
//    [self.navigationController pushViewController:mapVC animated:YES];
    MDDEditAddressViewController * eavc = mGetStoryboardViewController(@"Main", @"mDDEditAddressViewController");
    NavigationController_pushVC(eavc);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    mNavitationTitle(@"我的收货地址");
    [self customNavigationItems];
    [self addRightBarButtonItemWithTitle:@"新增" action:@selector(rightAction:)];
    self.listArr = [[NSMutableArray alloc]init];
    
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self requestAddressList];
}
#pragma mark -
#pragma mark - **************** tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.listArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AddressListCell * listCell = mGetTableViewCell(@"addressListCell");
    AddressListModel * itemModel = self.listArr[indexPath.section];
    listCell.nameLabel.text = itemModel.name;
    listCell.phoneLabel.text = itemModel.phone;
    listCell.detailLabel.text = [NSString stringWithFormat:@"%@%@%@%@",itemModel.province,itemModel.city,itemModel.county,itemModel.detailsAddress];
    listCell.itemModel = itemModel;
    if (itemModel.type.integerValue == 1) {
        listCell.morenBtn.selected = YES;
        [listCell.morenBtn setTitleColor:[UIColor AxcUI_colorWithHexCode:@"#4693CD"] forState:UIControlStateNormal];
    }else{
        listCell.morenBtn.selected = NO;
        [listCell.morenBtn setTitleColor:[UIColor AxcUI_colorWithHexCode:@"#b4b4b4"] forState:UIControlStateNormal];
    }
    listCell.btnActionReturnBlock = ^(MyAddressListBtnType type) {
        if (type == MyAddressListBtnEdit) {//编辑
            MDDEditAddressViewController * eavc = mGetStoryboardViewController(@"Main", @"mDDEditAddressViewController");
            eavc.addressModel = itemModel;
            NavigationController_pushVC(eavc);
        }
        if (type == MyAddressListBtnDel) {//删除
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"摩嗒嗒提醒您" message:@"是否删除该地址,删除后不可恢复" preferredStyle: UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
                
            }]];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSMutableDictionary * requestInfo = [[NSMutableDictionary alloc]init];
                setDickeyobj(requestInfo, itemModel.aid, @"aid");
                setDickeyobj(requestInfo, @"1", @"add");
                [PPNetworkHelper POST:app_person_address_delete parameters:requestInfo success:^(id responseObject) {
                    if (YJPPSUCCESS(responseObject)) {
                        [self.listArr removeObject:itemModel];
                    }else{
                        
                    }
                    [self.selfTableView reloadData];
                } failure:^(NSError *error) {
                    
                } showLoading:nil];
            }]];
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
        }
        if (type == MyAddressListBtnNormal) {//默认
            
        }
        if (type == MyAddressListBtnSelted) {//设为默认地址
            NSMutableDictionary * requestInfo = [[NSMutableDictionary alloc]init];
            setDickeyobj(requestInfo, itemModel.aid, @"aid");
            setDickeyobj(requestInfo, @"1", @"add");
            [PPNetworkHelper POST:app_person_address_default parameters:requestInfo success:^(id responseObject) {
                if (YJPPSUCCESS(responseObject)) {
                    [self requestAddressList];
                }else{
                    
                }
            } failure:^(NSError *error) {
                
            } showLoading:nil];
        }
    };
    return listCell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.returnAddressBlock) {
        self.returnAddressBlock(self.listArr[indexPath.section]);
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 8;
}
#pragma mark -
#pragma mark - **************** 数据请求
-(void)requestAddressList
{
    [self.listArr removeAllObjects];
    NSMutableDictionary * requestInfo = [[NSMutableDictionary alloc]init];
    [PPNetworkHelper POST:app_person_address_list parameters:requestInfo success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            for (NSDictionary * infoDic in responseObject[@"data"]) {
                AddressListModel * model = [[AddressListModel alloc]init];
                [model setValuesForKeysWithDictionary:infoDic];
                [self.listArr addObject:model];
            }
        }else{
            
        }
        [self.selfTableView reloadData];
    } failure:^(NSError *error) {
        
    } showLoading:nil];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
